<template>
  <div id="mainRP" style="height: 300px; width: 100%"></div>
</template>

<script>
import * as echarts from 'echarts';
import axios from "axios";

export default {
  name: "companyStatisticsRightUp",
  methods: {
    fetchJobs() {
      return axios.get(`/api/jobs/find/${this.$store.state.companyId}`).then(resp => {
        const jobsData = resp.data;
        const salaryRangeCount = {
          "3k-4k": 0,
          "4k-5k": 0,
          "5k-6k": 0,
          "6k-7k": 0,
          "7k-8k": 0,
          ">8k": 0,
        };
        jobsData.forEach(job => {
          const averageSalary = (job.salaryL + job.salaryR) / 2; // 计算平均薪资
          const salaryRange = getSalaryRange(averageSalary); // 获取薪资区间
          if (salaryRangeCount[salaryRange]) {
            salaryRangeCount[salaryRange]++;
          } else {
            salaryRangeCount[salaryRange] = 1;
          }
        });
        return Object.keys(salaryRangeCount).map(key => ({
          value: salaryRangeCount[key],
          name: key
        }));
      });
    }
  },
  mounted() {
    this.fetchJobs().then(newData => {
      const chartDom = document.getElementById('mainRP');
      const myChart = echarts.init(chartDom);
      const option = {
        grid: {
          left: '50%',
          top: '50%',
          width: '80%',
          height: '80%',
          containLabel: true,
          transform: 'translate(-50%, -50%)'
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          orient: 'vertical',
          left: 'left'
        },
        series: [
          {
            name: 'Salary Range',
            type: 'pie',
            radius: '80%',
            data: newData,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      };

      myChart.setOption(option);
    }).catch(error => {
      console.error('获取职位数据出错：', error);
    });
  }
}

function getSalaryRange(salary) {
  if (salary >= 3 && salary < 4) {
    return "3k-4k";
  } else if (salary >= 4 && salary < 5) {
    return "4k-5k";
  } else if (salary >= 5 && salary < 6) {
    return "5k-6k";
  } else if (salary >= 6 && salary < 7) {
      return "6k-7k";
  } else if (salary >= 7 && salary < 8) {
    return "7k-8k";
  }else if (salary >= 8) {
    return ">8k";
  }
  return ""; // 如果不在指定区间，返回空字符串（可根据实际需求调整处理方式）
}
</script>

<style scoped>
</style>
